package com.mdd.front.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.GreetingCardsRecord;
import com.mdd.common.entity.GreetingCardsSet;
import com.mdd.common.entity.user.User;
import com.mdd.common.mapper.GreetingCardsRecordMapper;
import com.mdd.common.mapper.GreetingCardsSetMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.em.EMServiceUtil;
import com.mdd.front.service.IGreetingCardsService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.vo.greetingCards.WishVo;

import cn.hutool.core.collection.CollectionUtil;

@Service
public class GreetingCardsServiceImpl implements IGreetingCardsService{

	@Autowired
	private GreetingCardsSetMapper greetingCardsSetMapper;
	@Autowired
	private GreetingCardsRecordMapper greetingCardsRecordMapper;
	@Autowired
	private EMServiceUtil eMServiceUtil;
	@Autowired
	private UserMapper userMapper;

	@Override
	public List<GreetingCardsSet> greetingCardList() {
		List<GreetingCardsSet> list = greetingCardsSetMapper.selectList(new LambdaQueryWrapper<GreetingCardsSet>());
		return CollectionUtil.isEmpty(list)?new ArrayList<GreetingCardsSet>():list;
	}

	@Override
	public AjaxResult<Object> giveGreetingCards(Integer userId, GreetingCardsRecord card) {
		User currUser = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, userId)
				.select(User::getId,User::getEmUsername,User::getNickname,User::getAvatar));
		if(null == currUser) {
			return AjaxResult.failed("用户不存在");
		}
		User toUser = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, card.getToUserId())
				.select(User::getId,User::getEmUsername,User::getNickname));
		if(null == toUser) {
			return AjaxResult.failed("用户不存在");
		}
		GreetingCardsSet cardSet = greetingCardsSetMapper.selectOne(new LambdaQueryWrapper<GreetingCardsSet>()
				.eq(GreetingCardsSet::getId, card.getCardId()));
		if(null == cardSet) {
			return AjaxResult.failed("贺卡不存在");
		}
		GreetingCardsRecord record = new GreetingCardsRecord();
		record.setType(1);
		record.setCardId(card.getCardId());
		record.setUserId(userId);
		record.setToUserId(card.getToUserId());
		record.setContent(card.getContent());
		record.setCreateTime(TimeUtils.timestamp());
		greetingCardsRecordMapper.insert(record);
		record = new GreetingCardsRecord();
		record.setType(2);
		record.setCardId(card.getCardId());
		record.setUserId(card.getToUserId());
		record.setToUserId(userId);
		record.setContent(card.getContent());
		record.setCreateTime(TimeUtils.timestamp());
		greetingCardsRecordMapper.insert(record);
		// 发送环信自定义消息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("currNickName", toUser.getNickname());
		map.put("img", cardSet.getImg());
		map.put("title", cardSet.getType());
		map.put("nickName", currUser.getNickname());
		map.put("headPic", currUser.getAvatar());
		map.put("content", card.getContent());
		eMServiceUtil.sendCustomMsg(currUser.getEmUsername(), toUser.getEmUsername(), "giveGreetingCards", map);
		return AjaxResult.success();
	}

	@Override
	public PageResult<WishVo> wishList(Integer userId, Integer type, Integer toUserId, PageValidate pageValidate) {
		Integer pageNo = pageValidate.getPageNo();
		Integer pageSize = pageValidate.getPageSize();
		MPJQueryWrapper<GreetingCardsRecord> mpjQueryWrapper = new MPJQueryWrapper<>();
		
		mpjQueryWrapper.select("t.id, t.type, t.content, DATE_FORMAT(FROM_UNIXTIME(t.create_time),'%m月%d日 %H:%i') as time, gcs.type as title, "
				+ "gcs.img, cu.nickname as currNickName, tu.nickname as nickName, tu.avatar as headPic ");
		mpjQueryWrapper.leftJoin("?_greeting_cards_set gcs on gcs.id = t.card_id".replace("?_", GlobalConfig.tablePrefix));
		mpjQueryWrapper.leftJoin("?_user cu on cu.id = t.user_id".replace("?_", GlobalConfig.tablePrefix));
		mpjQueryWrapper.leftJoin("?_user tu on tu.id = t.to_user_id".replace("?_", GlobalConfig.tablePrefix));
		if(type > 0) {
			mpjQueryWrapper.eq("t.type",type);
		}
//		mpjQueryWrapper.eq("t.user_id",userId);
		mpjQueryWrapper.and(i ->i.eq("t.user_id", userId).or().eq("t.user_id", toUserId));
		mpjQueryWrapper.and(i ->i.eq("t.to_user_id", userId).or().eq("t.to_user_id", toUserId));
		mpjQueryWrapper.orderByDesc("t.create_time");
		IPage<WishVo> iPage = greetingCardsRecordMapper.selectJoinPage( new Page<>(pageNo, pageSize), WishVo.class, mpjQueryWrapper);
		return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
		
	}


}
